﻿#include "function_blob_size.h"

void blob (HObject ho_image, HObject *ho_region, HObject *ho_error_region, HTuple hv_threshold,
    HTuple hv_error_size, HTuple hv_error_length, HTuple *hv_size)
{

  // Local iconic variables
  HObject  ho_Region, ho_ConnectedRegions, ho_SelectedRegions;
  HObject  ho_ConnectedRegions1, ho_SelectedRegions1, ho_RegionFillUp;
  HObject  ho_RegionErosion, ho_region0, ho_ConnectedRegions2;
  HObject  ho_error_region1, ho_error_region2;

  // Local control variables
  HTuple  hv_Width, hv_Height, hv_Row, hv_Column;

  GetImageSize(ho_image, &hv_Width, &hv_Height);


  Threshold(ho_image, &ho_Region, 0, hv_threshold);
  Connection(ho_Region, &ho_ConnectedRegions);
  SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "height", "and", 1500, 300000);
  Connection(ho_SelectedRegions, &ho_ConnectedRegions1);
  SelectShapeStd(ho_ConnectedRegions1, &ho_SelectedRegions1, "max_area", 70);

  FillUp(ho_SelectedRegions1, &ho_RegionFillUp);
  ErosionRectangle1(ho_RegionFillUp, &ho_RegionErosion, 300, 300);


  Difference(ho_RegionErosion, ho_SelectedRegions, &ho_region0);



  Connection(ho_region0, &ho_ConnectedRegions1);

  SelectShape(ho_ConnectedRegions1, &ho_ConnectedRegions1, "area", "and", 100, 99999999);

  Union1(ho_ConnectedRegions1, &(*ho_region));
  AreaCenter((*ho_region), &(*hv_size), &hv_Row, &hv_Column);

  Connection((*ho_region), &ho_ConnectedRegions2);
  SelectShape(ho_ConnectedRegions2, &ho_error_region1, "area", "and", hv_error_size,
      99999999);

  SelectShape(ho_ConnectedRegions2, &ho_error_region2, "max_diameter", "and", hv_error_length,
      99999999);

  ConcatObj(ho_error_region1, ho_error_region2, &(*ho_error_region));

  return;
}

FunctionBlobSize::FunctionBlobSize(QDialog* parent, QSettings* qsettings, QString section,int camera_index) : QDialog(parent)
{
	

    m_qsettings = qsettings;
    m_section = section;

    //this->setWindowTitle(("Dump"));
    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width()/1920.0;
    double ratio_y = desktop_rect.height()/1080.0;

    move(0,0);
    setFixedSize(1920*ratio_x,1010*ratio_y);

    m_view = new QtQGraphicsView(this);
    m_view->setGeometry(660 * ratio_x, 0, 1250 * ratio_x, 940 * ratio_y);




    QSize control_size(150*ratio_x,40*ratio_y);

    QWidget* p = new QWidget(this);
    p->move(0*ratio_x,0*ratio_y);
    p->setFixedSize(600*ratio_x,800*ratio_y);

    QGridLayout *qgridLayout = new QGridLayout();
    p->setLayout(qgridLayout);

    int index = 0;

    QSize ButtonSize(220 * ratio_x, 40 * ratio_y);


	QLabel* p_qlabel;
	p_qlabel = new QLabel(this);
	p_qlabel->setText("标准面积");
	p_qlabel->setFixedSize(ButtonSize);
	qgridLayout->addWidget(p_qlabel, index, 0);
	m_qspinbox_standard_size = new QSpinBox();
    m_qspinbox_standard_size->setFixedSize(ButtonSize);
	m_qspinbox_standard_size->setMinimum(0);
	m_qspinbox_standard_size->setMaximum(10000000);
	qgridLayout->addWidget(m_qspinbox_standard_size, index, 1);
	index++;




    p_qlabel = new QLabel(this);
    p_qlabel->setText("比例");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_ratio = new QDoubleSpinBox();
	m_qspinbox_ratio->setFixedSize(ButtonSize);
    m_qspinbox_ratio->setMinimum(0);
    m_qspinbox_ratio->setMaximum(1);
    qgridLayout->addWidget(m_qspinbox_ratio, index, 1);
    index++;

    p_qlabel = new QLabel(this);
	p_qlabel->setText("灰度阈值");
	p_qlabel->setFixedSize(ButtonSize);
	qgridLayout->addWidget(p_qlabel, index, 0);
	m_qspinbox_threshold_gray = new QSpinBox();
    m_qspinbox_threshold_gray->setFixedSize(ButtonSize);
	m_qspinbox_threshold_gray->setMinimum(100);
	m_qspinbox_threshold_gray->setMaximum(254);
	qgridLayout->addWidget(m_qspinbox_threshold_gray, index, 1);
	index++;

    p_qlabel = new QLabel(this);
    p_qlabel->setText("单个阈值面积");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_error_size = new QSpinBox();
    m_qspinbox_error_size->setFixedSize(ButtonSize);
    m_qspinbox_error_size->setMinimum(1);
    m_qspinbox_error_size->setMaximum(1000000);
    qgridLayout->addWidget(m_qspinbox_error_size, index, 1);
    index++;

    p_qlabel = new QLabel(this);
    p_qlabel->setText("标准长度");
    p_qlabel->setFixedSize(ButtonSize);
    qgridLayout->addWidget(p_qlabel, index, 0);
    m_qspinbox_standard_length = new QSpinBox();
    m_qspinbox_standard_length->setFixedSize(ButtonSize);
    m_qspinbox_standard_length->setMinimum(0);
    m_qspinbox_standard_length->setMaximum(10000000);
    qgridLayout->addWidget(m_qspinbox_standard_length, index, 1);
    index++;

    m_pushbutton_save_parameter = new QPushButton(this);
    m_pushbutton_save_parameter->setFixedSize(ButtonSize);
    m_pushbutton_save_parameter->setText("保存");
    connect(m_pushbutton_save_parameter, &QPushButton::clicked, this, &FunctionBlobSize::saveParameter);
    qgridLayout->addWidget(m_pushbutton_save_parameter, index,0);

    index++;
	m_pushbutton_show_orign_image = new QPushButton(this);
    m_pushbutton_show_orign_image->setFixedSize(ButtonSize);
    m_pushbutton_show_orign_image->setText("显示原图");
	connect(m_pushbutton_show_orign_image, &QPushButton::clicked, this, [=]() {
		QImage q_image = matToQImage(m_image);
        m_view->setImage(q_image, "",Qt::red, 20);
		return true;
	});
	qgridLayout->addWidget(m_pushbutton_show_orign_image, index, 0);

	m_pushbutton_detection = new QPushButton(this);
    m_pushbutton_detection->setFixedSize(ButtonSize);
    m_pushbutton_detection->setText("检测");
	connect(m_pushbutton_detection, &QPushButton::clicked, this, &FunctionBlobSize::detectionImage);
	qgridLayout->addWidget(m_pushbutton_detection, index, 1);
    
    index++;
    m_pushbutton_return = new QPushButton(this);
    m_pushbutton_return->setFixedSize(ButtonSize);
    m_pushbutton_return->setText("Exit");
    qgridLayout->addWidget(m_pushbutton_return, index, 0);
    connect(m_pushbutton_return, &QPushButton::clicked, this, [=]() {
        this->hide();
        });
    loadParameter();
}

FunctionBlobSize::~FunctionBlobSize()
{
    ;
}

bool FunctionBlobSize::detection( HObject image_in, QString& message)
{
    m_size =0;
    Mat mat_image;


    try
    {

        HTuple htuple_size;
        blob(image_in,&m_region, &m_error_region,m_standard_threshold_gray,m_error_size,m_standard_length,&htuple_size);
        m_size =  htuple_size[0].I();
        message = "像素面积" + QString::number(m_size)+"\n百分比"+QString::number(m_size*100/m_standard_size);

		if (m_size < m_standard_size*m_ratio)
		{
            message += "\n小于标准面积*比例"+QString::number(m_standard_size*m_ratio) + "\n";;
			return false;
		}

        if (m_size < m_standard_size*m_ratio)
        {
            message += "\n小于标准面积*比例"+QString::number(m_standard_size*m_ratio) + "\n";;
            return false;
        }

        HTuple hv_Length;
        CountObj(m_error_region, &hv_Length);
        int value = hv_Length[0].I();
        if(value>0)
        {
            message += "缺料";
            return false;
        }

    }
    catch (HException &HDevExpDefaultException)
    {
        message = HDevExpDefaultException.ErrorMessage();

        return false;
    }

    return true;
}

void FunctionBlobSize::detectionImage()
{
    if(m_image.empty())
    {
        QMessageBox::information(nullptr,"","no image");
        return;
    }

    QString message;
    HObject image = matToHImage(m_image);
    bool state = detection(image, message);

	

    Mat image_show = m_image.clone();
    if(image_show.empty())
    {
        QMessageBox::information(nullptr,"","no image");
        return;
    }
    draw(image_show, image_show);

    QImage q_image = matToQImage(image_show);

	if (state)
		m_view->setImage(q_image, message, Qt::green, 60);
	else
		m_view->setImage(q_image, message, Qt::red, 60);

    
    
    }

void FunctionBlobSize::draw(Mat image_in,Mat& image_out)
{


    if (image_in.channels() == 1)
    {
        cvtColor(image_in, image_in, COLOR_GRAY2BGR);
    }
    image_out = drawRegion(image_in, m_region, Scalar(0, 255, 0));

    GenEmptyRegion(&m_region);

    image_out = drawRegion(image_out, m_error_region, Scalar(0, 0, 255));



    GenEmptyRegion(&m_error_region);

}

void FunctionBlobSize::loadParameter()
{

	


	m_standard_size = m_qsettings->value(m_section + "/" + "m_standard_size", 1).toInt();
	m_standard_threshold_gray = m_qsettings->value(m_section + "/" + "m_standard_threshold_gray").toInt();

    m_standard_length = m_qsettings->value(m_section + "/" + "m_standard_length", 1).toInt();


	m_ratio = m_qsettings->value(m_section + "/" + "m_ratio",1).toDouble();

    m_error_size = m_qsettings->value(m_section + "/" + "m_error_size",1).toDouble();

	m_qspinbox_standard_size->setValue(m_standard_size);
    m_qspinbox_standard_length->setValue(m_standard_length);

	m_qspinbox_threshold_gray->setValue(m_standard_threshold_gray);
    m_qspinbox_error_size->setValue(m_error_size);

    m_qspinbox_ratio->setValue(m_ratio);

    return ;
}

void FunctionBlobSize::saveParameter()
{



	m_standard_size = m_qspinbox_standard_size->value();
	m_standard_threshold_gray = m_qspinbox_threshold_gray->value();

	m_standard_length = m_qspinbox_standard_length->value();
    m_error_size = m_qspinbox_error_size->value();

    m_ratio = m_qspinbox_ratio->value();



		m_qsettings->setValue(m_section + "/" + "m_standard_size", m_standard_size);
        m_qsettings->setValue(m_section + "/" + "m_standard_length", m_standard_length);

		m_qsettings->setValue(m_section + "/" + "m_standard_threshold_gray", m_standard_threshold_gray);

        m_qsettings->setValue(m_section + "/" + "m_error_size", m_error_size);

        m_qsettings->setValue(m_section + "/" + "m_ratio", m_ratio);





    
    return;
}

bool FunctionBlobSize::showDialog()
{
    QImage q_image = matToQImage(m_image);
    m_view->setImage(q_image, "",Qt::red, 20);
   
    return true;
}


